package com.mine.yyb.modules.sys.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mine.yyb.common.utils.exception.MineErrorCode;
import com.mine.yyb.common.utils.exception.MineException;
import com.mine.yyb.common.utils.json.MineJacksonkMapper;
import com.mine.yyb.common.web.YybBaseController;
import com.mine.yyb.modules.sys.bo.UserBO;
import com.mine.yyb.modules.sys.enums.SysCodeEnum;
import com.mine.yyb.modules.sys.service.IUserService;
import com.mine.yyb.modules.sys.vo.UserVO;

/**
 * <pre>
 * <一句话功能简述>
 * 
 * 注册功能
 * 
 * <功能详细描述>
 * </pre>
 * 
 * @author xuganhua
 * @version [版本号, 2016年5月23日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Controller
public class RegisterControl extends YybBaseController
{
    @Autowired
    private IUserService iUserService;
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 进入注册页面
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2016年5月21日]
     * @param request
     * @param response
     * @return
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping(value = "/initRegisterPage")
    public String initRegisterPage(HttpServletRequest request, HttpServletResponse response)
    {
        getResponseHeader(response);
        return "/html/register";
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 进入注册确认页面页面
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2016年5月21日]
     * @param request
     * @param response
     * @return
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping(value = "/initRegisterConfirmPage")
    public String initRegisterConfirmPage(HttpServletRequest request, HttpServletResponse response)
    {
        getResponseHeader(response);
        return "/html/confirm";
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 判断用户名是否已经存在
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2015年12月13日]
     * @param request
     * @param userBO
     * @return
     * @throws MineException
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping(value = "/checkUsername")
    public ResponseEntity<String> checkUsername(HttpServletRequest request, @ModelAttribute UserBO userBO)
        throws MineException
    {
        boolean result = true;// 表示用户名已经存在
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        map.put("valid", result);
        try
        {
            logger.info(userBO.toString());
            
            // 判断用户名是否为空
            if (StringUtils.isBlank(userBO.getUsername()))
            {
                throw new MineException(SysCodeEnum.ERROR_USERNAME_BLANK);
            }
            
            result = iUserService.isUsernameExist(userBO);
            map.put("valid", !result);
        }
        catch (MineException e)
        {
            logger.error(e.getErrorCode().name(), e);
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
        }
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.set("Content-Type", "application/json; charset=utf-8");
        return new ResponseEntity<String>(MineJacksonkMapper.convertObjectToString(map), responseHeaders,
            HttpStatus.OK);
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 判断邮箱是否已经存在
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2015年12月13日]
     * @param request
     * @param userBO
     * @return
     * @throws MineException
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping(value = "/checkEmail")
    public ResponseEntity<String> checkEmail(HttpServletRequest request, @ModelAttribute UserBO userBO)
        throws MineException
    {
        boolean result = true;// 表示邮箱已经存在
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        map.put("valid", result);
        try
        {
            logger.info(userBO.toString());
            
            // 判断邮箱是否为空
            if (StringUtils.isBlank(userBO.getEmail()))
            {
                throw new MineException(SysCodeEnum.ERROR_EMAIL_BLANK);
            }
            
            result = iUserService.isEmailExist(userBO);
            map.put("valid", !result);
        }
        catch (MineException e)
        {
            logger.error(e.getErrorCode().name(), e);
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
        }
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.set("Content-Type", "application/json; charset=utf-8");
        return new ResponseEntity<String>(MineJacksonkMapper.convertObjectToString(map), responseHeaders,
            HttpStatus.OK);
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 注册用户信息
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2015年12月13日]
     * @param request
     * @param userBO
     * @return
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping(value = "/register")
    public ResponseEntity<String> registerUser(HttpServletRequest request, @ModelAttribute UserBO userBO)
    {
        // 响应码
        String retCode = null;
        UserVO userVO = null;
        try
        {
            logger.info(userBO.toString());
            // 判断用户名是否为空
            if (StringUtils.isBlank(userBO.getUsername()))
            {
                throw new MineException(SysCodeEnum.ERROR_USERNAME_BLANK);
            }
            // 判断密码是否为空
            if (StringUtils.isBlank(userBO.getPassword()))
            {
                throw new MineException(SysCodeEnum.ERROR_PASSWORD_BLANK);
            }
            // 判断邮箱是否为空
            if (StringUtils.isBlank(userBO.getEmail()))
            {
                throw new MineException(SysCodeEnum.ERROR_EMAIL_BLANK);
            }
            // 判断邮箱是否为空
            if (StringUtils.isBlank(userBO.getQq()))
            {
                throw new MineException(SysCodeEnum.ERROR_QQ_BLANK);
            }
            userVO = iUserService.registerUser(userBO);
            if (userVO == null)
            {
                retCode = MineErrorCode.ERROR_INTERNAL.name();
            }
            else
            {
                retCode = MineErrorCode.SUCCESS.name();
            }
        }
        catch (MineException e)
        {
            logger.error(e.getErrorCode().name(), e);
            retCode = e.getErrorCode().name();
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
            retCode = MineErrorCode.ERROR_INTERNAL.name();
        }
        return getResponseEntity(retCode, userVO);
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 重新发送邮件
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2016年1月21日]
     * @param request
     * @param userBO
     * @return
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping(value = "/reSendEmail")
    public ResponseEntity<String> reSendEmail(HttpServletRequest request, @ModelAttribute UserBO userBO)
    {
        // 响应码
        String retCode = null;
        try
        {
            logger.info(userBO.toString());
            // 判断用户名是否为空
            if (StringUtils.isBlank(userBO.getInnerID()))
            {
                throw new MineException(SysCodeEnum.ERROR_INNERID_BLANK);
            }
            // 判断邮箱是否为空
            if (StringUtils.isBlank(userBO.getEmail()))
            {
                throw new MineException(SysCodeEnum.ERROR_EMAIL_BLANK);
            }
            iUserService.reSendEmail(userBO);
            retCode = MineErrorCode.SUCCESS.name();
        }
        catch (MineException e)
        {
            logger.error(e.getErrorCode().name(), e);
            retCode = e.getErrorCode().name();
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
            retCode = MineErrorCode.ERROR_INTERNAL.name();
        }
        return getResponseEntity(retCode);
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 激活用户
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2015年12月13日]
     * @param request
     * @param userBO
     * @return
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping(value = "/activate")
    public ResponseEntity<String> activateUser(HttpServletRequest request, @ModelAttribute UserBO userBO)
    {
        String retCode = null;
        try
        {
            logger.info(userBO.toString());
            
            // 判断用户内部ID是否为空
            if (StringUtils.isBlank(userBO.getInnerID()))
            {
                throw new MineException(SysCodeEnum.ERROR_INNERID_BLANK);
            }
            
            retCode = iUserService.activateUser(userBO);
        }
        catch (MineException e)
        {
            logger.error(e.getErrorCode().name(), e);
            retCode = e.getErrorCode().name();
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
            retCode = MineErrorCode.ERROR_INTERNAL.name();
        }
        return getResponseEntity(retCode);
    }
}
